﻿// Copyright (c) 2007-2021 西安交通信息投资营运有限公司 版权所有
// 作者:吕艳阳

using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using XCI.Core;
using XCI.Extensions;
using XCI.Helper;

namespace XCI.Setting
{
    public class ServerSettingStore : ISettingStore
    {
        /// <summary>
        /// 加载配置
        /// </summary>
        /// <param name="target">配置目标</param>
        /// <param name="id">配置标识</param>
        /// <param name="name">配置名称</param>
        public Task LoadAsync(ISettingSerializer target, string id, string name)
        {
            return LoadCoreAsync(target, id, name);
        }

        /// <summary>
        /// 保存配置
        /// </summary>
        /// <param name="target">配置目标</param>
        /// <param name="id">配置标识</param>
        /// <param name="name">配置名称</param>
        public Task SaveAsync(ISettingSerializer target, string id, string name)
        {
            return SaveCoreAsync(target, id, name);
        }

        /// <summary>
        /// 复制配置
        /// </summary>
        /// <param name="id">配置标识</param>
        /// <param name="sourceName">源名称</param>
        /// <param name="destName">目标名称</param>
        public Task CopyAsync(string id, string sourceName, string destName)
        {
            var sourceServerFile = CrreateServerFile(id, sourceName);
            return sourceServerFile.CopyAsync(destName);
        }

        /// <summary>
        /// 下载配置
        /// </summary>
        /// <param name="id">配置标识</param>
        /// <param name="name">配置名称</param>
        public Task DownloadAsync(string id, string name)
        {
            var sourceServerFile = CrreateServerFile(id, name);
            return sourceServerFile.DownloadAsync();
        }

        /// <summary>
        /// 删除配置
        /// </summary>
        /// <param name="id">配置标识</param>
        /// <param name="name">配置名称</param>
        public Task DeleteAsync(string id, string name)
        {
            var serverFile = CrreateServerFile(id, name);
            return serverFile.DeleteAsync();
        }

        /// <summary>
        /// 查询配置名称集合
        /// </summary>
        /// <param name="id">配置标识</param>
        public async Task<IList<string>> ListAsync(string id)
        {
            var serverFile = CrreateServerFile(id, "def.xml");
            var fileList = await serverFile.ListAsync();
            var list = new List<string>();
            if (fileList.IsListNotEmpty())
            {
                foreach (var item in fileList)
                {
                    if (string.IsNullOrEmpty(item.Name)) continue;
                    list.Add(Path.GetFileNameWithoutExtension(item.Name));
                }
            }
            return list;
        }

        /// <summary>
        /// 创建服务器数据文件
        /// </summary>
        /// <param name="id">配置标识</param>
        /// <param name="name">配置名称</param>
        public ServerFile CrreateServerFile(string id, string name)
        {
            string localCatalog = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, AppDefaultSettings.LayoutStoreLocalRootCatalog, id);
            string serverCatalog = $"/{AppDefaultSettings.LayoutStoreServerRootCatalog}/{id}";
            return new ServerFile(localCatalog, serverCatalog, $"{name}.xml");
        }

        protected virtual async Task LoadCoreAsync(ISettingSerializer target, string id, string name)
        {
            var serverFile = CrreateServerFile(id, name);
            await serverFile.DownloadAsync();
            if (File.Exists(serverFile.LocalFilePath))
            {
                target.RestoreLayout(serverFile.LocalFilePath);
            }
        }

        protected virtual async Task SaveCoreAsync(ISettingSerializer target, string id, string name)
        {
            var serverFile = CrreateServerFile(id, name);
            IOHelper.CreateDirectoryByPath(serverFile.LocalFilePath);
            target.SaveLayout(serverFile.LocalFilePath);
            await serverFile.UploadAsync();
        }
    }
}